package logic

import (
	"Junior_two/subject/goods/goodsrpc/pb/goodRpc"
	"context"
	"net/http"

	"Junior_two/subject/goods/goodsapi/internal/svc"
	"Junior_two/subject/goods/goodsapi/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsAddLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGoodsAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsAddLogic {
	return &GoodsAddLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GoodsAddLogic) GoodsAdd(req *types.GoodsRequest) (resp *types.GoodsResponse, err error) {
	// todo: add your logic here and delete this line
	res, err := l.svcCtx.GoodServer.GoodsAdd(l.ctx, &goodRpc.AddRequest{
		Name:   req.Name,
		Price:  req.Price,
		TypeId: req.TypeId,
		Status: req.Status,
		Images: req.Img,
		Desc:   req.Desc,
		Number: req.Num,
	})
	if err != nil {
		return &types.GoodsResponse{
			Msg:  err.Error(),
			Code: http.StatusAccepted,
		}, nil
	}
	if res.Code != http.StatusOK {
		return &types.GoodsResponse{
			Msg:  res.Msg,
			Code: res.Code,
		}, nil
	}
	return &types.GoodsResponse{
		Msg:  res.Msg,
		Code: res.Code,
	}, nil
}
